package com.foodlocker.machine;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class KeyboardUtil {
	private Context ctx;
	private Activity act;
	private KeyboardView keyboardView;
//	private Keyboard k1;// 字母键盘
	private Keyboard k2;// 数字键盘
//	public boolean isnun = false;// 是否数据键盘
//	public boolean isupper = false;// 是否大写

	private EditText pass1;
	private EditText pass2;
	private EditText pass3;
	private EditText pass4;
	private EditText pass5;
	private EditText pass6;

	public KeyboardUtil(Activity act, Context ctx, EditText pass1, EditText pass2,EditText pass3,EditText pass4,EditText pass5,EditText pass6) {
		this.act = act;
		this.ctx = ctx;
		this.pass1 = pass1;
		this.pass2 = pass2;
		this.pass3 = pass3;
		this.pass4 = pass4;
		this.pass5 = pass5;
		this.pass6 = pass6;
//		k1 = new Keyboard(ctx, R.xml.qwerty);
		k2 = new Keyboard(ctx, R.xml.symbols);
		keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
		keyboardView.setKeyboard(k2);
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(true);
		keyboardView.setOnKeyboardActionListener(listener);
	}

	private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
		@Override
		public void swipeUp() {
		}

		@Override
		public void swipeRight() {
		}

		@Override
		public void swipeLeft() {
		}

		@Override
		public void swipeDown() {
		}

		@Override
		public void onText(CharSequence text) {
		}

		@Override
		public void onRelease(int primaryCode) {
		}

		@Override
		public void onPress(int primaryCode) {
		}

		@Override
		public void onKey(int primaryCode, int[] keyCodes) {
			
		//	if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
		//		hideKeyboard();
		//	} else 
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
				
				if(pass2.isFocused()) { pass1.setText(""); pass1.requestFocus(); return;}
				if(pass3.isFocused()) {  pass2.setText(""); pass2.requestFocus(); return;}
				if(pass4.isFocused()) {  pass3.setText(""); pass3.requestFocus(); return;}
				if(pass5.isFocused()) { pass4.setText(""); pass4.requestFocus(); return;}
				if(pass6.isFocused()) {  pass5.setText(""); pass5.requestFocus(); return;}
				pass1.setText(""); pass1.requestFocus(); return;
				}
			 
				if (primaryCode == 57419) { // go left 冲定义为清空
			
				pass1.setText("");
				pass2.setText("");
				pass3.setText("");
				pass4.setText("");
				pass5.setText("");
				pass6.setText("");
				pass1.requestFocus();
			}  else {
			
			
				if(pass1.isFocused()) { pass1.setText(Chr(primaryCode)); return; }
				if(pass2.isFocused()) { pass2.setText(Chr(primaryCode)); return;}
				if(pass3.isFocused()) { pass3.setText(Chr(primaryCode)); return;}
				if(pass4.isFocused()) { pass4.setText(Chr(primaryCode)); return;}
				if(pass5.isFocused()) { pass5.setText(Chr(primaryCode)); return;}
				if(pass6.isFocused()) { pass6.setText(Chr(primaryCode)); return;} 
				
				
			}
		}
	};
	

    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }
    
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.INVISIBLE);
        }
    }
    
    private boolean isword(String str){
    	String wordstr = "abcdefghijklmnopqrstuvwxyz";
    	if (wordstr.indexOf(str.toLowerCase())>-1) {
			return true;
		}
    	return false;
    }
    public static String Chr(int asciiCode)
    {
 
      char c;
     
      c= (char)asciiCode;
   
      String strCharacter = String.valueOf(c);
      Log.i("Number", strCharacter);
      return strCharacter;
     
    
    }

}

